package com.youdianstar.app.common.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.youdianstar.app.common.ex.getVmClazz
import com.youdianstar.app.common.util.dialog.DialogUtil



/**
 * File: BaseVmFragment
 * Author: 82149 Create: 2021/9/18 19:12
 * Changes (from 2021/9/18)
 * --------------------------------------------------
 * description: 两种设置 binding 的方式
 * ---------------------------------------------------
 *
 */
abstract class BaseBMFragment<DB : ViewDataBinding,VM : BaseViewModel> : Fragment() {
    //private lateinit var mBaseContainBinding: BaseFragmentLayoutBinding
    protected lateinit var binding: DB
    protected lateinit var viewModel: VM

    private var viewModelId: Int? = null
    lateinit var mActivity: AppCompatActivity

    abstract fun initVariableId(): Int?

    //abstract fun getLayoutId(): Int
    abstract fun initContentView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): Int


    /**创建ViewModel 方法0:配合koin使用,需要网络接口时*/
    abstract fun initViewModel(): VM?

    /**创建ViewModel 方法1:无网络接口时*/
    private fun createViewModel(): VM {
        return ViewModelProvider(this).get(getVmClazz(this))
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.unbind()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        inflater.let {
            //mBaseContainBinding = DataBindingUtil.inflate(inflater, R.layout.base_fragment_layout, container, false)
            //binding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false)
            binding = DataBindingUtil.inflate(
                it,
                initContentView(it, container, savedInstanceState),
                container,
                false
            )
        }
        viewModelId = initVariableId()
        viewModel = if (null == initViewModel()) createViewModel() else initViewModel()!!
        //关联viewModel
        viewModelId?.let { binding.setVariable(it, viewModel) }
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        registerUIChangeLiveDataCallBack()
        initData()
        initClickEvent()
        initLiveObserver()
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as AppCompatActivity
    }

    private var time: Long = 0
    private var oldMsg: String? = null

    /**
     * 相同msg 只显示一个。
     * @param msg 消息
     */
    fun showToast(msg: String) {
        if (msg != oldMsg) {
            Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show()
            time = System.currentTimeMillis()
        } else {
            if (System.currentTimeMillis() - time > 2000) {
                Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show()
                time = System.currentTimeMillis()
            }
        }
        oldMsg = msg
    }


    private fun registerUIChangeLiveDataCallBack(){

    }

    abstract fun initData()

    abstract fun initClickEvent()

    abstract fun initLiveObserver()

    /**
     * dialog展示
     */
    fun showLoadingDialog(title: String) {
        DialogUtil.instance.showLoading(activity?.baseContext!!)
    }

    /**
     * 取消
     */
    fun dismissDialog() {
        DialogUtil.instance.dismissLoading()
    }
}